package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsRegisterUser;
import com.caiyi.financial.nirvana.sms.entity.SmsUser;
import com.caiyi.financial.nirvana.sms.querybean.QueryUserBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface SmsRegisterUserMapper extends BaseMapper<SmsRegisterUser> {


    @SelectProvider(type = SmsRegisterUserMapper.UserSqlProvider.class, method = "selectRegisterUserList")
    List<SmsRegisterUser> selectRegisterUserList(QueryUserBean bean);

    @Update("update sms_register_user set status=#{status} where id=#{id} and status = 1")
    int updateStatus(@Param("status") Integer status, @Param("id") Integer id);

    @Update("update sms_register_user set status=#{status},remark = #{remark} where id=#{id} and status = 1")
    int reject(@Param("status") Integer status, @Param("id") Integer id, @Param("remark") String remark);

    @Select("select id,phone,company_name,name,create_time,update_time,status,remark from sms_register_user where id=#{id}")
    SmsRegisterUser getRegisterUser(@Param("id") Integer id);

    class UserSqlProvider {
        public String selectRegisterUserList(QueryUserBean bean) {
            return new SQL() {
                {
                    SELECT("id,phone,company_name,name,create_time,update_time,status,remark");
                    FROM("sms_register_user");
                    if (!StringUtils.isEmpty(bean.getCompanyName())) {
                        WHERE("company_name LIKE concat('%',#{companyName},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getName())) {
                        WHERE("name LIKE concat('%',#{name},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getPhone())) {
                        WHERE("phone=#{phone}");
                    }
                    if (bean.getStatus() != null) {
                        WHERE("status=#{status}");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");

                }
            }.toString();
        }
    }

}
